import ScrollGridView from "../TFrame/script/ui/ScrollGridView";
import Coordinates from "./Coordinates";
import IconFramesMgr from "./IconFramesMgr";
import NativeCaller from "./NativeCaller";
import PlayerDataMgr from "./PlayerDataMgr";
import ResManager from "./ResManager";
import Util from "../../StartAb/Util";
import TurnTableSystem from "./system/TurnTableSystem";
import CoinSystem from "./system/CoinSystem";
import GardenSystem from "./garden/GardenSystem";
import IAPSystem from "./system/IAPSystem";
import WXAD from "../../StartAb/WXAd";

const { ccclass, property } = cc._decorator;

const LevelNodePicKey = 'LevelNodePicKey';

@ccclass
export default class Select extends cc.Component {

    @property(ScrollGridView)
    scrollGridView: ScrollGridView = null;

    @property(cc.Node)
    scrollContentNode: cc.Node = null

    @property(cc.AudioClip)
    selectAudio: cc.AudioClip = null;


    start() {
        let selectScrollOffYStr = cc.sys.localStorage.getItem("selectScrollOffY")
        if (selectScrollOffYStr) {
            this.scheduleOnce(() => {
                let selectScrollOffY = parseInt(selectScrollOffYStr)
                this.scrollContentNode.position = cc.v3(this.scrollContentNode.position.x, selectScrollOffY)
                cc.log("selectScrollOff:%s", selectScrollOffY)

                this.scrollGridView.itemCount = this.getLevelCount();
            }, 0)
        } else {
            this.scrollGridView.itemCount = this.getLevelCount();
        }
        NativeCaller.showBanner();
    }

    getLevelCount() {
        return ResManager.getLevelResArray().length;
    }

    createEvent(cellNode: cc.Node) {
        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node
        clickEventHandler.component = "Select"; //脚本名字
        clickEventHandler.handler = "levelSelect"; //点击的回调函数
        var btn = cellNode.addComponent(cc.Button);
        btn.clickEvents.push(clickEventHandler);
        btn.pressedColor = cc.color(200, 200, 200)
    }

    //i是界面显示序号，从0开始
    itemRenderEvent(cellNode: cc.Node, i: number) {
        let levelIndex = i;
        let lastIndex = PlayerDataMgr.getInstance().getLastLevelIndex()
        let textNode = cellNode.getChildByName("lt").getChildByName('num')
        if (textNode) {
            textNode.getComponent(cc.Label).string = (levelIndex + 1).toString()
            textNode.setAnchorPoint(cc.v2(0.5, 0.5))
            if (lastIndex >= 0 && levelIndex == lastIndex) {
                textNode.color = cc.Color.BLUE
            } else {
                textNode.color = cc.Color.BLACK;
            }
        }

        cellNode['levelIndex'] = levelIndex;

        let resName = ResManager.getLevelResNameByIndex(i);
        let starCount = PlayerDataMgr.getInstance().getLevelStar(levelIndex);
        // let starCount = 3 - rank
        let iconNode = cellNode.getChildByName('icon')
        if (iconNode) {
            let spr = iconNode.getComponent(cc.Sprite) || iconNode.addComponent(cc.Sprite);
            let sf = IconFramesMgr.getInstance().getFrameByName(resName)
            spr.spriteFrame = sf
            if(sf == null)
                console.error("没找到icon:", resName)
            // let mat = iconNode.getComponent(cc.Sprite).getMaterial(0);
            // mat.define("USE_MASAIC", starCount == 0);
        }

        cellNode[LevelNodePicKey] = resName;

        for (let i = 1; i <= 3; ++i) {
            let node = cellNode.getChildByName('stars').getChildByName(i.toString());
            (node) && (node.active = i <= starCount);
        }

        let maskNode = cellNode.getChildByName("mask");
        maskNode && (maskNode.active = (starCount == 0))
    }


    levelSelect(e) {
        cc.log("levelSelect:%s", e.target.name)
        let btn: cc.Node = e.target
        Util.disableNodeButton(e.target)
        // this.node.getChildByName("block").active = true
        let levelIndex = btn['levelIndex']
        cc.sys.localStorage.setItem("selectScrollOffY", this.scrollContentNode.position.y)
        Coordinates.loadLvCoordinate(levelIndex, () => {
            ResManager.currentLevelIndex = levelIndex
            PlayerDataMgr.getInstance().setLastLevelIndex(levelIndex)
            Util.changeScene(PlayerDataMgr.isADVideo ? "game_ad" : "game");
            WXAD.instance.hideBanner();
        })
        this.selectAudio && cc.audioEngine.playEffect(this.selectAudio, false);
    }
}
